package com.ChefApp.mobile;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Vorschau  {
	String subtitle,recUrl,title,ratingUrl,date,diff,time,picUrl;
	TextView tv1,tv2,tv3,tv4;
	ImageView iv1,iv2;
	LinearLayout linhori,linver1, linver2;
	TableRow tr;
	TableLayout tl;
	TextView tvZutaten, tvTitle, tvSubTitle, tvZubereitung;
	ImageView preImage, ivRec;
	EditText umrET;
	Bitmap picBmp;
	public Vorschau(Element item, TableLayout tl){
		this.tl = tl;
		Elements rows=item.children();
		
		//Get the strings
		if (!(rows.get(1).childNode(1).attr("title").equals(""))){
		this.subtitle=rows.get(1).childNode(1).attr("title");
		}
		
		if (!(rows.get(1).childNode(1).attr("href").equals(""))){
		this.recUrl="http://www.chefkoch.de"+rows.get(1).childNode(1).attr("href");
		}
		if (!( rows.get(1).childNode(1).childNode(0).childNodes().equals(""))){
		this.title=rows.get(1).childNode(1).childNode(0).childNode(0).toString();
		}
		if (!( rows.get(2).childNode(0).attr("src").equals(""))){
		this.ratingUrl=rows.get(2).childNode(0).attr("src");
		}
		if (!( rows.get(1).childNode(5).childNodes().equals(""))){
		this.date=rows.get(1).childNode(5).childNode(0).toString();
		}
		if (!( rows.get(3).childNodes().equals(""))){
		this.diff=rows.get(3).childNode(2).toString();
		this.time=rows.get(3).childNode(0).toString();
		}
		if (!( rows.get(0).childNode(1).attr("src").equals(""))){
		this.picUrl=rows.get(0).childNode(1).attr("src");
		}
		
		 tr = new TableRow(tl.getContext());
		 linhori =new LinearLayout(tr.getContext());
		linhori.setOrientation(LinearLayout.HORIZONTAL);
		
		linver1=new LinearLayout(linhori.getContext());
		linver1.setOrientation(LinearLayout.VERTICAL);
		
		 linver2=new LinearLayout(linhori.getContext());
		linver2.setOrientation(LinearLayout.VERTICAL);
		
		 tv1 = new TextView(linver1.getContext());
		 tv2 = new TextView(linver1.getContext());
		 tv3 = new TextView(linver1.getContext());
		 tv4 = new TextView(linver1.getContext());
		 iv1 = new ImageView(linhori.getContext());
		 iv2 = new ImageView(linhori.getContext());
		
		picBmp=LoadImage(this.getPicUrl());
		Bitmap rateBmp=LoadImage(this.getRatingUrl());
		
		iv1.setImageBitmap(picBmp);
		iv2.setImageBitmap(rateBmp);
		tv1.setText(this.getTitle());
		tv2.setText(this.getSubtitle());
		tv3.setText(this.getDiff());
		tv4.setText(this.getTime());
		
		tv1.setWidth(150);
		tv2.setWidth(150);
		
		iv1.setClickable(false);
		iv2.setClickable(false);
		tv1.setClickable(false);
		tv2.setClickable(false);
		tv3.setClickable(false);
		tv4.setClickable(false);
		
		tr.setClickable(true);
		/*tr.setOnClickListener(new View.OnClickListener(){

			

			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				Receipe rec= new Receipe(recUrl);
				tvZutaten= (TextView) findViewById(R.id.textViewrec4);
				tvTitle = (TextView) findViewById(R.id.textViewrec1);
				tvSubTitle = (TextView) findViewById(R.id.textViewrec2);
				tvTitle.setText(getTitle());
				tvSubTitle.setText(getSubtitle());
				tvZutaten.setText(rec.zutatenText.getText());
				tvZubereitung =(TextView) act.findViewById(R.id.textViewrecZubereitung1);
				tvZubereitung.setText(rec.getZubereitungText());
				ivRec=(ImageView) act.findViewById(R.id.imageViewrec1);
				ivRec.setImageBitmap(picBmp);
				umrET=(EditText) act.findViewById(R.id.editTextrec1);
				umrET.setText(rec.getPortion());
			}
			
		});*/
			
			
		
		
	}
	
	public void addViews() {
		// TODO Auto-generated method stub
		linver1.addView(tv1,0);
		linver1.addView(tv2, 1);
		
		linver2.addView(tv3, 0);
		linver2.addView(tv4, 1);
		linver2.addView(iv2, 2);
		
		linhori.addView(iv1, 0);
		linhori.addView(linver1, 1);
		linhori.addView(linver2, 2);
		
		tr.addView(linhori);
		tl.addView(tr);
	}

	private Bitmap LoadImage(String imageUrl){
		Bitmap bitmap = null;
		try {
			  
			  bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());

			} catch (MalformedURLException e) {
			  e.printStackTrace();
			} catch (IOException e) {
			  e.printStackTrace();
			}
			  return bitmap;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public String getRecUrl() {
		return recUrl;
	}

	public String getTitle() {
		return title;
	}

	public String getRatingUrl() {
		return ratingUrl;
	}

	public String getDate() {
		return date;
	}

	public String getDiff() {
		return diff;
	}

	public String getTime() {
		return time;
	}

	public String getPicUrl() {
		return picUrl;
	}

}
